/**
 * 
 */
package br.com.locasis.conversores;

import java.math.BigDecimal;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

/**
 * @author robert.resende
 * 
 */
public class FloatMascaraConverter implements Converter {

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext , javax.faces.component.UIComponent, java.lang.String)
	 */
	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		Float instanciaFloat = null;

		if (value == null || value.trim().length() == 0)
			return instanciaFloat;

		value = value.replace(".", "");
		value = value.replace(",", ".");
		instanciaFloat = new Float(value);
		return instanciaFloat;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext , javax.faces.component.UIComponent, java.lang.Object)
	 */
	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) {
		if (!(value instanceof Float)) {
			return "";
		}
		return ((BigDecimal) value).toString();
	}

}
